<template>
  <div class="home">
    <nav-bar class="home-nav"><div slot="center">蘑菇街</div></nav-bar>
    <refresh v-show="isShow"></refresh>

    <tab-control
      class="tab-control"
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      v-show="isTabFixed"
      ref="tabControl1"
    ></tab-control>

    <scroll
      class="content"
      ref="scroll"
      :pull-up-load="true"
      :probe-type="3"
      @scroll="contentScroll"
      @pullingUp="loadMore"
    >
      <home-swiper
        :banners="banners"
        @swiperImageLoad="swiperImageLoad"
      ></home-swiper>

      <recommend-view :recommends="recommends"></recommend-view>

      <feature-view></feature-view>

      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2"
      ></tab-control>

      <goods-list :goods="showGoods"></goods-list>
    </scroll>

    <back-top @backClick="backClick" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
// 表头
import NavBar from "components/common/navbar/NavBar";
// 轮播图
import HomeSwiper from "./childComps/HomeSwiper.vue";
// 本周流行
import FeatureView from "./childComps/FeatureView.vue";
// 流行新款精选
import TabControl from "components/content/tabControl/TabControl";
// 推荐页信息查询
import RecommendView from "./childComps/RecommendView.vue";
// 商品栏
import GoodsList from "components/content/goods/GoodsList.vue";

// BScroll滚动
import Scroll from "components/common/scroll/Scroll.vue";

// 请求数据
import { getHomeMultidata, getHomeGoods } from "network/home";

// 工具js 防抖与过滤器
// import { debounce } from "common/utils.js";

// 混入mixin
import {
  backTopMixin,
  itemListenermixin,
  tabControlMixin,
} from "common/mixin.js";

export default {
  name: "Home",
  components: {
    NavBar,
    HomeSwiper,
    FeatureView,
    TabControl,
    RecommendView,
    GoodsList,
    Scroll,
  },
  mixins: [backTopMixin, itemListenermixin, tabControlMixin],
  data() {
    return {
      // 储存请求的数据
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },

      // tabcontrol的显示
      isTabFixed: false,
      // tabcontrol2的位置
      tabOffsetTop: 0,
      // 滚动位置
      pageY: 0,

      currentType: "pop",

      // 记住离开home页面时的位置
      saveY: 0,
    };
  },

  created() {
    this._getHomeMultidata();

    this._getHomeGoods("pop");
    this._getHomeGoods("new");
    this._getHomeGoods("sell");
  },

  // 进入页面的时候(活跃) 跳转到记住的位置
  activated() {
    // 从商品详细切换时 记住home所在位置
    this.$refs.scroll.scroll.scrollTo(0, -this.saveY, 0);
    this.$refs.scroll.scroll.refresh();
  },

  // 离开页面的时候(不活跃) 记住位置
  deactivated() {
    this.saveY = this.$refs.scroll.scroll.getScrollY;

    // 取消全局事件的监听 $off取消
    this.$bus.$off("itemImageLoad", this.itemImgListener);
  },

  destroyed() {
    console.log("home被销毁");
  },

  // DOM加载完后
  mounted() {
    // 接收$bus总线传出的事件
    // 监听30张商品图片是否加载完毕并执行刷新;
    // 每完成一张 便执行一次 一次性执行30次太过频繁
    // 所以需要用到防抖操作 debounce
    // const refresh = debounce(this.$refs.scroll.refresh, 100);

    // this.itemImgListener = () => {
    //   refresh();
    // };

    // // 解决上拉加载更多无法计算滚动长度问题
    // this.$bus.$on("itemImageLoad", this.itemImgListener);

    this.itemListenermixin;
    // 代替1
    this.tabControlMixin;
  },

  methods: {
    /*
    // 点击事件
    */
    // 代替1
    // tabClick(index) {
    //   switch (index) {
    //     case 0:
    //       this.currentType = "pop";
    //       break;
    //     case 1:
    //       this.currentType = "new";
    //       break;
    //     case 2:
    //       this.currentType = "sell";
    //       break;
    //   }
    //   this.$refs.tabControl1.currentIndex = index;
    //   this.$refs.tabControl2.currentIndex = index;
    // },

    // tabControl的位置
    // 当轮播图加载好的时候 再获取tabControl的位置
    swiperImageLoad() {
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },

    contentScroll(position) {
      // console.log(position.y);
      this.pageY = -position.y;

      // tabcontrol的显示
      this.isTabFixed = this.pageY > this.tabOffsetTop;

      // backtop的显示
      this.listenShowBackTop(position);

      // 上拉刷新
      this.topRefresh(position);
    },

    /*
    // 请求数据
    */
    _getHomeMultidata() {
      getHomeMultidata().then((res) => {
        // console.log(res);

        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },

    // type是哪一栏
    // page是那一页
    _getHomeGoods(type) {
      // 每调用一次加一页
      const page = this.goods[type].page + 1;

      getHomeGoods(type, page).then((res) => {
        // console.log(res);
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;

        this.$refs.scroll.scroll.finishPullUp();
      });
    },

    // 上拉加载更多
    loadMore() {
      this._getHomeGoods(this.currentType);
    },
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
};
</script>

<style>
.home {
  height: 100vh;
  position: relative;
  background: url("../../assets/img/regisetr/login2.jpg");
}

.home-nav {
  background-color: var(--color-tint);

  color: white;
  font-size: 18px;
  font-weight: bold;
  letter-spacing: 3px;
}

.content {
  overflow: hidden;

  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}

.tab-control {
  position: relative;
  z-index: 8;
}
</style>
